import { apiLogin } from '@/api/user';
import type { UStoreState } from '@/types/users';
import { createPinia, defineStore } from 'pinia';

export const userStore = defineStore('userStore', {
    persist: {
        key: 'userStoreKey',
        paths: [
            'userInfo', 'token'
        ]
    },
    state: (): UStoreState => {
        return {
          // 所有这些属性都将自动推断其类型
          userInfo: {},
          token: ''
        }
    },
    actions: {
        login() {
            uni.login({
                success: async (res) => {
                    console.log(res);
                    if (res.code) {
                        //发起网络请求
                        const data = await apiLogin(res.code);
                        console.log(data, '--------123');
                      this.userInfo = data.data.user;
                      this.token = data.data.token;
                    } else {
                        console.log('登录失败！' + res.errMsg)
                    }
                }
            })
        }
    }
})